#include "radio_list.h"
#include <stdlib.h>
#include <string.h>
#include "ui.h"


// 页面创建函数（返回自定义结构体指针）
RadioListPage* create_radio_list_page(
    lv_obj_t *parent,
    const char *label_title,
    const char *items[],
    int item_count,
    int default_index
) {
    LV_LOG_USER("Font ptr = %p", &ui_font_ALIMAMA18bpp4);
    LV_LOG_USER("Font ptr = %p", &ui_font_ALIMAMA14bpp4);
    if (item_count > MAX_LIST_ITEMS) item_count = MAX_LIST_ITEMS;

    // 分配页面数据结构
    RadioListPage *page = lv_mem_alloc(sizeof(RadioListPage));
    memset(page, 0, sizeof(RadioListPage));
    page->item_count = item_count;

    // 创建焦点组
    page->group = lv_group_create();

    // 创建容器
    page->container = lv_obj_create(parent);
    lv_obj_clear_flag(page->container, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_size(page->container, 128, 128);
    lv_obj_set_style_bg_color(page->container, lv_color_black(), LV_PART_MAIN);
    lv_obj_set_style_border_width(page->container, 0, LV_PART_MAIN);

    // 标题
    lv_obj_t *title = lv_label_create(page->container);
    lv_label_set_text(title, label_title);
    lv_obj_set_style_text_font(title, &ui_font_ALIMAMA18bpp4, LV_PART_MAIN);
    lv_obj_set_style_text_color(title, lv_color_white(), LV_PART_MAIN);
    lv_obj_align(title, LV_ALIGN_TOP_LEFT, 0, 0);

    // 创建列表
    lv_obj_t *list = lv_list_create(page->container);
    lv_obj_set_size(list, 128, 70);
    lv_obj_align(list, LV_ALIGN_BOTTOM_LEFT, -10, -10);
    lv_obj_set_style_bg_color(list, lv_color_black(), LV_PART_MAIN);
    lv_obj_set_style_border_width(list, 0, LV_PART_MAIN);

    // 初始化样式
    static lv_style_t style_checkbox;
    lv_style_init(&style_checkbox);
    lv_style_set_text_font(&style_checkbox, &ui_font_ALIMAMA14bpp4);
    lv_style_set_text_color(&style_checkbox, lv_color_white());


    // 创建checkbox项
    for (int i = 0; i < item_count; i++) {
        lv_obj_t *cb = lv_checkbox_create(list);
        lv_checkbox_set_text(cb, items[i]);
        lv_obj_add_style(cb, &style_checkbox, LV_PART_MAIN);

        // 存储到页面结构
        page->checkbox_array[i] = cb;

        // 设置布局
        lv_obj_set_style_pad_left(cb, 0, LV_PART_MAIN);
        lv_obj_set_style_pad_top(cb, 2, LV_PART_MAIN);
        lv_obj_set_style_pad_bottom(cb, 2, LV_PART_MAIN);

        // 加入 group
        lv_group_add_obj(page->group, cb);
    }


    // 设置默认选中项及焦点
    if (default_index >= 0 && default_index < item_count) {
        for (int j = 0; j < item_count; j++) {
            lv_obj_clear_state(page->checkbox_array[j], LV_STATE_CHECKED);  // 取消选中
            lv_obj_set_style_text_color(page->checkbox_array[j], lv_color_white(), LV_PART_MAIN);
        }

        // 设置当前选中项
        lv_obj_add_state(page->checkbox_array[default_index], LV_STATE_CHECKED);  // 设置选中
        lv_obj_set_style_text_color(page->checkbox_array[default_index], lv_color_hex(0x00FF00), LV_PART_MAIN);

        // 设置焦点
        page->focus_index = default_index;
        lv_group_focus_obj(page->checkbox_array[default_index]);
        lv_obj_scroll_to_view(page->checkbox_array[default_index], LV_ANIM_OFF);
    }


    return page;
}

void radio_list_handle_key(RadioListPage *list, uint32_t key) {
    //LV_LOG_USER("Key event: %d (current focus: %d)", key, list->focus_index);
    int i = list->focus_index;

    if (key == LV_KEY_DOWN) {
        i = (i + 1) % list->item_count;
    } else if (key == LV_KEY_UP) {
        i = (i - 1 + list->item_count) % list->item_count;
    }

    // 更新焦点索引
    list->focus_index = i;

    // 设置选中状态和样式
    for (int j = 0; j < list->item_count; j++) {
        lv_obj_clear_state(list->checkbox_array[j], LV_STATE_CHECKED);
        lv_obj_set_style_text_color(list->checkbox_array[j], lv_color_white(), LV_PART_MAIN);
    }
    lv_obj_add_state(list->checkbox_array[i], LV_STATE_CHECKED);
    lv_obj_set_style_text_color(list->checkbox_array[i], lv_color_hex(0x00FF00), LV_PART_MAIN);

    // 焦点与滚动
    lv_group_focus_obj(list->checkbox_array[i]);
    lv_obj_scroll_to_view(list->checkbox_array[i], LV_ANIM_ON);
}

